<script setup lang="ts">
import { onBeforeMount, ref } from "vue";
import { formRules } from "./utils/rule";
import { FormProps } from "./utils/types";
import { useColumns } from "./utils/columns";
import ReCol from "@/components/ReCol";
import { getWarehouseShowApi } from "@/api/warehouseApi";
const props = withDefaults(defineProps<FormProps>(), {
  formInline: () => ({
    title: "新增",
    status: 1,
    code: "",
    name: "",
    warehouseId: 0,
    warehouse: "",
    goodsLocationId: 0
  })
});
const statusOptions = [
  {
    value: 1,
    label: "启用"
  },
  {
    value: 2,
    label: "禁用"
  }
];
const ruleFormRef = ref();
const newFormInline = ref(props.formInline);

function getRef() {
  return ruleFormRef.value;
}
const { columns, pagination, selectValue, dataList, rowStyle } = useColumns();
/** 行点击 */
const selectRef = ref();
function onRowClick(row) {
  selectValue.value = row.name;
  selectRef.value.blur();
  newFormInline.value.warehouseId = row.warehouseId;
}
onBeforeMount(() => {
  getWarehouseShowApi({ warehouseId: newFormInline.value.warehouseId }).then(
    result => {
      selectValue.value = result.result.name;
    }
  );
});
defineExpose({ getRef });
</script>

<template>
  <el-form
    ref="ruleFormRef"
    :model="newFormInline"
    :rules="formRules"
    label-width="auto"
  >
    <el-row :gutter="30">
      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="仓库名称">
          <el-select
            ref="selectRef"
            v-model="selectValue"
            class="!w-[600px]"
            placeholder="请选择"
            value-key="corpId"
            clearable
          >
            <template #empty>
              <div class="m-4">
                <pure-table
                  row-key="corpId"
                  alignWhole="center"
                  :header-cell-style="{
                    background: 'var(--el-fill-color-light)',
                    color: 'var(--el-text-color-primary)'
                  }"
                  :row-style="rowStyle"
                  :data="dataList"
                  :columns="columns"
                  :pagination="pagination"
                  @row-click="onRowClick"
                />
              </div>
            </template>
          </el-select>
        </el-form-item>
      </re-col>
      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="库位名称" prop="name">
          <el-input
            v-model="newFormInline.name"
            clearable
            placeholder="请输入库位名称"
          />
        </el-form-item>
      </re-col>
      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="库位编号" prop="code">
          <el-input
            v-model="newFormInline.code"
            clearable
            placeholder="请输入库位编号"
          />
        </el-form-item>
      </re-col>
      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="状态" prop="status">
          <el-select
            v-model="newFormInline.status"
            placeholder="请选择状态"
            class="w-full"
            clearable
          >
            <el-option
              v-for="(item, index) in statusOptions"
              :key="index"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
      </re-col>
    </el-row>
  </el-form>
</template>
<style scoped></style>
